package com.example.demo.service.impl;

import com.example.demo.common.Result;
import com.example.demo.mapper.StatisticMapper;
import com.example.demo.mapper.VehicleMapper;
import com.example.demo.service.StatisticsService;
import com.example.demo.vo.StatisticTableData;
import com.example.demo.vo.StatisticTableData2;
import com.example.demo.vo.TypeAndSum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private StatisticMapper mapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public Result getRidesOnRoute() {
        List<Map<Long, Integer>> mapList = mapper.getRidesOnRoute();
//        System.out.println(mapList);
        StatisticTableData std = new StatisticTableData();
        String[] xAxis = new String[mapList.size()];
        Long[] yAxis = new Long[mapList.size()];
        int i = 0;
        for(Map map : mapList) {
            xAxis[i] = String.valueOf(map.get("routeId"));
            yAxis[i] = (Long)map.get("y");
            i++;
        }
        std.setXAxis(xAxis);
        std.setYAxis(yAxis);
        return Result.success(std);
    }

    @Override
    public Result getRidesOnTime(String checiId) {
        List<Map> mapList = mapper.getRidesOnTime(checiId);
        long[] yAxis = new long[6];
        for(Map map : mapList) {
            System.out.println(map);
            switch ((String)map.get("tp")){
                case "group1": yAxis[0] = (Long)map.get("orderCnt"); break;
                case "group2": yAxis[1] = (Long)map.get("orderCnt"); break;
                case "group3": yAxis[2] = (Long)map.get("orderCnt"); break;
                case "group4": yAxis[3] = (Long)map.get("orderCnt"); break;
                case "group5": yAxis[4] = (Long)map.get("orderCnt"); break;
                case "group6": yAxis[5] = (Long)map.get("orderCnt"); break;
            }
        }
        return Result.success(yAxis);
    }

    @Override
    public Result getSumOfTypeOnRoute() {
        // 查询有哪些车型
        List<String> types = vehicleMapper.selectTypes();

        TypeAndSum[] yAxisList = new TypeAndSum[types.size()];

        StatisticTableData2 result = new StatisticTableData2();

        int j = 0;
        for(String type : types) {
            List<Map<Long, Integer>> mapList = mapper.getSumOfTypeOnRoute(type);

            String[] xAxis = new String[mapList.size()];
            Long[] data = new Long[mapList.size()];
            int i = 0;
            for(Map map : mapList) {
                xAxis[i] = String.valueOf(map.get("routeId"));
                data[i] = (Long)map.get("y");
                i++;
            }
            //取第一次获得到的横轴
            if(result.getXAxis() == null || result.getXAxis().length < xAxis.length)
                result.setXAxis(xAxis);
            TypeAndSum yAxis = new TypeAndSum(type, "bar", data);

            yAxisList[j] = yAxis;
            j++;
        }

        result.setYAxis(yAxisList);
        return Result.success(result);
    }
}
